﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Timers;
using Joshi.Utils.Imap;
using System.Collections;
using System.Xml;
using System.IO;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

            aTimer = new System.Timers.Timer();
            this.ActiveControl = textBox2;
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
            this.textBox3.TextChanged += new System.EventHandler(this.textBox3_TextChanged);
            this.textBox4.TextChanged += new System.EventHandler(this.textBox4_TextChanged);
            this.button1.Click += new System.EventHandler(this.button1_Click);
            AcceptButton = button1;
            this.Resize += new System.EventHandler(Form1_Resize);
            this.notifyIcon1.DoubleClick += new EventHandler(this.notifyIcon1_MouseDoubleClick);
            this.button2.Click += new EventHandler(this.button2_Click);         
        }

        public void Form1_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
                Hide();
        }
        
        public void notifyIcon1_MouseDoubleClick(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }

        public void button1_Click(object sender, EventArgs e)
        {
            string latencyStr = textBox2.Text.ToString();
            int latency = Convert.ToInt32(latencyStr);
            
            textBox2.Enabled = false;
            if (textBox1.Text.Length > 0)
            {
                textBox1.Enabled = false;
                textBox2.Enabled = false;
                textBox3.Enabled = false;
                textBox4.Enabled = false;
                checkBox1.Enabled = false;
                button2.Enabled = true;
                button1.Enabled = false;
                aTimer.AutoReset = true;
                aTimer.Interval = latency;
                aTimer.Enabled = true;
                label1.Text = "";
            }
        }

        private Color m_col = Color.White;
        private Font m_font;

        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            switch(newl)
            {
                case 1:
                    mrwiggles1();
                    break;
                case 2:
                    mrwiggles2();
                    break;
            }  
        }

        void mrwiggles1()
        {
            Imap oImap = new Imap();

            string sHost = textBox3.Text;

            string sUser = textBox4.Text;

            string sPwd = textBox1.Text;

            string usessl = null;

            if (checkBox1.Checked)
            {
                usessl = "true";
            }
            else
            {
                usessl = "false";
            }

            string sInbox = "INBOX";
            //login and extract unread count
            try
            {
                oImap.Login(sHost, sUser, sPwd, usessl.Trim().ToLower().Equals("true") ? true : false);
                oImap.SelectFolder(sInbox);
            }
            catch (ImapException e)
            {
                if (e.Type == ImapException.ImapErrorEnum.IMAP_ERR_LOGIN)
                {
                    if (this.InvokeRequired)
                    {
                        BeginInvoke(new MethodInvoker(delegate()
                        {
                            aTimer.Enabled = false;
                            label1.ForeColor = Color.Red;
                            label1.Text = "auth\nfailure";
                            oImap.Disconnect();
                            button2.Enabled = false;
                            textBox1.Enabled = true;
                            textBox2.Enabled = true;
                            textBox3.Enabled = true;
                            textBox4.Enabled = true;
                            checkBox1.Enabled = true;
                            textBox1.Text = "";
                            label2.Text = "unread count";
                            button2.Enabled = false;
                            textBox1.Focus();
                        }
                            ));
                    }
                    return;
                }
                BeginInvoke(new MethodInvoker(delegate()
                {
                    aTimer.Enabled = false;
                    label1.ForeColor = Color.Red;
                    label1.Text = "connection\nfailure";
                    oImap.Disconnect();
                    button2.Enabled = false;
                    textBox1.Enabled = true;
                    textBox1.Text = "";
                    textBox2.Enabled = true;
                    textBox3.Enabled = true;
                    textBox3.Text = "";
                    textBox4.Enabled = true;
                    textBox4.Text = "";
                    checkBox1.Enabled = true;
                    textBox1.Text = "";
                    label2.Text = "unread count";
                    button2.Enabled = false;
                    textBox3.Focus();
                }
                ));
                
                return;
            }

            oImap.LogOut();

            oImap.Disconnect();

            //push unread count up to ui thread
            BeginInvoke(new MethodInvoker(delegate()
            {
                //Dispose1();
                m_font = new Font("Microsoft Sans Serif", 10);
                label2.Text = oImap.sUIDPart;
                notifyIcon1.Text = oImap.sUIDPart;
                ShowText(oImap.sUIDPart, m_font, m_col);
                if (oImap.sUIDPart == null)
                {
                    //Dispose1();
                    m_font = new Font("Microsoft Sans Serif", 10);
                    ShowText("0", m_font, m_col);
                    label2.Text = "0";
                    notifyIcon1.Text = "0";
                }
            }
                ));
        }
            //to send to case 2 (if needed)
            //newl = 2;

        private void mrwiggles2()
        {
            //to send back to case 1 (if needed)
            //newl = 1;
        }

        public void Dispose1()
        {
            notifyIcon1.Dispose();
            if (m_font != null)
            {
                m_font.Dispose();
            }
        }
        //create memory leak!
        public void ShowText(string text, Font font, Color col)
        {
            
            Brush brush = new SolidBrush(col);

            Bitmap bitmap = new Bitmap(16, 16);

            Graphics graphics = Graphics.FromImage(bitmap);

            graphics.DrawString(text, m_font, brush, 0, 0);

            // Convert the bitmap with text to an Icon

            IntPtr hIcon = bitmap.GetHicon();

            Icon icon = Icon.FromHandle(hIcon);

            notifyIcon1.Icon = icon;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            aTimer.Enabled = false;
            button2.Enabled = false;
            textBox1.Text = "";
            button2.Enabled = false;
            label1.Text = "";
            label2.Text = "";
            textBox1.Enabled = true;
            textBox2.Enabled = true;
            textBox3.Enabled = true;
            textBox4.Enabled = true;
            checkBox1.Enabled = true;
            textBox1.Focus();
        }

        public void textBox1_TextChanged(object sender, EventArgs e)
        {
            button1_enable();
        }
        public void textBox2_TextChanged(object sender, EventArgs e)
        {
            button1_enable();
        }
        public void textBox3_TextChanged(object sender, EventArgs e)
        {
            button1_enable();
        }
        public void textBox4_TextChanged(object sender, EventArgs e)
        {
            button1_enable();
        }
        //checks all text fields for input, then enables "Submit"
        public void button1_enable()
        {
            if (textBox1.Text.Length >= 1 && textBox2.Text.Length >= 1 && textBox3.Text.Length >= 1 && textBox4.Text.Length >= 1)
            {
                button1.Enabled = true;
            }
            else
            {
                button1.Enabled = false;

            }
        }
    }
}